/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mappers;

import handler.DatabaseConnection;
import java.io.FileNotFoundException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import model.Commodity;

/**
 *
 * @author Benjamin
 */
public class CommodityMapper {

    private static CommodityMapper instance;

    private CommodityMapper() {

    }

    public ArrayList<Commodity> getTable() throws SQLException, FileNotFoundException {

        ArrayList<Commodity> commodities = new ArrayList<>();
        Statement stmt = DatabaseConnection.getInstance().getConnection().createStatement();

        String SQL = "select * from commodity";
        ResultSet rs = stmt.executeQuery(SQL);

        while (rs.next()) {
            int id = rs.getInt("id");
            String cName = rs.getString("cName");
            int amount = rs.getInt("amount");
            int minAmount = rs.getInt("minAmount");

            commodities.add(new Commodity(id, cName, amount, minAmount, null));

        }

        //Giv råvarerne billeder.
        for (Commodity commodity : commodities) {
            commodity.setImage(assignPictureUrl(commodity));
        }

        rs.close();
        stmt.close();

        return commodities;
    }

    public Commodity getCommodity(int id) throws SQLException, FileNotFoundException {
        Statement stmt = DatabaseConnection.getInstance().getConnection().createStatement();

        String sql = "select * from commodity where id = " + id;
        ResultSet rs = stmt.executeQuery(sql);

        Commodity commodity = null;
        
        if (rs.next()) {
            int cId = rs.getInt("id");
            String cName = rs.getString("cName");
            int amount = rs.getInt("amount");
            int minAmount = rs.getInt("minAmount");
            
            commodity = new Commodity(id, cName, amount, minAmount, null);
        }
        
        commodity.setImage(assignPictureUrl(commodity));
        
        return commodity;
    }

    private String assignPictureUrl(Commodity commodity) {
        String url = "";
        switch (commodity.getId()) {
            case 1:
                url = "src/pictures/tshirt_white.png";
                break;
            case 4:
                url = "src/pictures/mug.png";
                break;
            case 7:
                url = "src/pictures/paper.png";
                break;
            case 10:
                url = "src/pictures/businessCards.png";
                break;
            case 16:
                url = "src/pictures/laminatrix.png";
                break;
            case 20:
                url = "src/pictures/binder.png";
                break;
            case 24:
                url = "src/pictures/ink.png";
                break;
        }
        return url;
    }

    public static CommodityMapper getInstance() {
        if (instance == null) {
            instance = new CommodityMapper();
        }
        return instance;
    }

}
